package com.lifeonwalden.dsg.service;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class DateServiceImpl implements DateService {
    private Set<Date> holidaySet = new HashSet<>();

    public DateServiceImpl() {
        holidaySet.add(new Date("2021/07/03"));
        holidaySet.add(new Date("2021/07/04"));
        holidaySet.add(new Date("2021/07/10"));
        holidaySet.add(new Date("2021/07/11"));
        holidaySet.add(new Date("2021/07/17"));
        holidaySet.add(new Date("2021/07/18"));
        holidaySet.add(new Date("2021/07/24"));
        holidaySet.add(new Date("2021/07/25"));
        holidaySet.add(new Date("2021/07/31"));
        holidaySet.add(new Date("2021/08/01"));
        holidaySet.add(new Date("2021/08/07"));
        holidaySet.add(new Date("2021/08/08"));
        holidaySet.add(new Date("2021/08/14"));
        holidaySet.add(new Date("2021/08/15"));
        holidaySet.add(new Date("2021/08/21"));
        holidaySet.add(new Date("2021/08/22"));
        holidaySet.add(new Date("2021/08/28"));
        holidaySet.add(new Date("2021/08/29"));
        holidaySet.add(new Date("2021/09/04"));
        holidaySet.add(new Date("2021/09/05"));
        holidaySet.add(new Date("2021/09/11"));
        holidaySet.add(new Date("2021/09/12"));
        holidaySet.add(new Date("2021/09/18"));
        holidaySet.add(new Date("2021/09/19"));
        holidaySet.add(new Date("2021/09/20"));
        holidaySet.add(new Date("2021/09/21"));
        holidaySet.add(new Date("2021/09/25"));
        holidaySet.add(new Date("2021/09/26"));
        holidaySet.add(new Date("2021/10/01"));
        holidaySet.add(new Date("2021/10/02"));
        holidaySet.add(new Date("2021/10/03"));
        holidaySet.add(new Date("2021/10/04"));
        holidaySet.add(new Date("2021/10/05"));
        holidaySet.add(new Date("2021/10/06"));
        holidaySet.add(new Date("2021/10/07"));
        holidaySet.add(new Date("2021/10/09"));
        holidaySet.add(new Date("2021/10/10"));
        holidaySet.add(new Date("2021/10/16"));
        holidaySet.add(new Date("2021/10/17"));
        holidaySet.add(new Date("2021/10/23"));
        holidaySet.add(new Date("2021/10/24"));
        holidaySet.add(new Date("2021/10/30"));
        holidaySet.add(new Date("2021/10/31"));
        holidaySet.add(new Date("2021/11/06"));
        holidaySet.add(new Date("2021/11/07"));
        holidaySet.add(new Date("2021/11/13"));
        holidaySet.add(new Date("2021/11/14"));
        holidaySet.add(new Date("2021/11/20"));
        holidaySet.add(new Date("2021/11/21"));
        holidaySet.add(new Date("2021/11/27"));
        holidaySet.add(new Date("2021/11/28"));
        holidaySet.add(new Date("2021/12/04"));
        holidaySet.add(new Date("2021/12/05"));
        holidaySet.add(new Date("2021/12/11"));
        holidaySet.add(new Date("2021/12/12"));
        holidaySet.add(new Date("2021/12/18"));
        holidaySet.add(new Date("2021/12/19"));
        holidaySet.add(new Date("2021/12/25"));
        holidaySet.add(new Date("2021/12/26"));
        holidaySet.add(new Date("2022/01/01"));
        holidaySet.add(new Date("2022/01/02"));
        holidaySet.add(new Date("2022/01/03"));
        holidaySet.add(new Date("2022/01/08"));
        holidaySet.add(new Date("2022/01/09"));
        holidaySet.add(new Date("2022/01/15"));
        holidaySet.add(new Date("2022/01/16"));
        holidaySet.add(new Date("2022/01/22"));
        holidaySet.add(new Date("2022/01/23"));
        holidaySet.add(new Date("2022/01/29"));
        holidaySet.add(new Date("2022/01/30"));
        holidaySet.add(new Date("2022/01/31"));
        holidaySet.add(new Date("2022/02/01"));
        holidaySet.add(new Date("2022/02/01"));
        holidaySet.add(new Date("2022/02/02"));
        holidaySet.add(new Date("2022/02/03"));
        holidaySet.add(new Date("2022/02/04"));
        holidaySet.add(new Date("2022/02/05"));
        holidaySet.add(new Date("2022/02/06"));
        holidaySet.add(new Date("2022/02/12"));
        holidaySet.add(new Date("2022/02/13"));
        holidaySet.add(new Date("2022/02/19"));
        holidaySet.add(new Date("2022/02/20"));
        holidaySet.add(new Date("2022/02/26"));
        holidaySet.add(new Date("2022/02/27"));
        holidaySet.add(new Date("2022/03/05"));
        holidaySet.add(new Date("2022/03/06"));
    }

    @Override
    public Date fetchNonHolidayOrNext(Date date) {
        if (holidaySet.contains(date)) {
            Instant instant = date.toInstant();
            do {
                instant = instant.plus(1, ChronoUnit.DAYS);
            } while (holidaySet.contains(Date.from(instant)));

            return Date.from(instant);
        }
        return date;
    }

    @Override
    public Date moveN(Date date, int n) {
        return null;
    }

    @Override
    public Date fetchNonHolidayOrPrevious(Date date) {
        if (holidaySet.contains(date)) {
            Instant instant = date.toInstant();
            do {
                instant = instant.minus(1, ChronoUnit.DAYS);
            } while (holidaySet.contains(Date.from(instant)));

            return Date.from(instant);
        }
        return date;
    }
}
